package com.example.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReflectUtil;
import com.graduation.pojo.BaseEntity;
import com.graduation.pojo.BaseRequest;
import com.graduation.vo.BaseVO;

import java.lang.reflect.Field;
import java.time.LocalDateTime;

public class BaseController<DO extends BaseEntity, VO extends BaseVO> {

    public VO getVoByDo(DO entity, Class<VO> clazz) {
        VO vo = BeanUtil.copyProperties(entity, clazz);
        Field[] fields = entity.getClass().getDeclaredFields();
        for (Field field: fields) {
            if ("LocalDateTime".equals(field.getType().getSimpleName())) {
                ReflectUtil.setFieldValue(vo, field.getName(), DateUtil.formatLocalDateTime((LocalDateTime) ReflectUtil.getFieldValue(entity, field)));
            }
        }
        vo.setCreateTime(DateUtil.formatLocalDateTime(entity.getCreateTime()));
        vo.setUpdateTime(DateUtil.formatLocalDateTime(entity.getUpdateTime()));
        buildVo(entity, vo);
        return vo;
    }

    public DO getDoByRequest(BaseRequest request, Class<DO> clazz) {
        return BeanUtil.copyProperties(request, clazz);
    }

    protected void buildVo(DO entity, VO vo) {

    }
}
